import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:management/model/open_gate_record_model.dart';
import 'package:management/utils/convert.dart';
import 'package:management/utils/custom_router.dart';
import 'package:management/views/mine/userInfo/open_gate_record.dart';

///查询开闸记录筛选条件
class SearchGateRecordCondition extends StatefulWidget {
  @override
  _SearchGateRecordConditionState createState() =>
      _SearchGateRecordConditionState();
}

class _SearchGateRecordConditionState extends State<SearchGateRecordCondition> {
  TextEditingController _beginTimeController = TextEditingController();
  TextEditingController _endTimeController = TextEditingController();
  TextEditingController _operatorController = TextEditingController();
  TextEditingController _passerbyController = TextEditingController();
  bool indicatorIsVisible = false;

  @override
  void initState() {
    super.initState();
    SystemChrome.setEnabledSystemUIOverlays([]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          iconSize: 35,
          onPressed: () => Navigator.of(context).pop(),
        ),
        title: Text(
          "筛选条件",
          style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
        ),
        centerTitle: true,
        actions: [
          IconButton(
              iconSize: 40,
              icon: Icon(Icons.search),
              onPressed: () {
                search();
              })
        ],
      ),
      body: _buildBody(),
    );
  }

  _buildBody() => Container(
        height: 300,
        padding: EdgeInsets.only(left: 20, right: 20, top: 40),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              height: 50,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Container(
                    child: Row(
                      children: [
                        Text(
                          "开始日期:",
                          style: TextStyle(
                            fontSize: 25,
                            fontWeight: FontWeight.bold,
                            color: Colors.blue,
                          ),
                        ),
                        Container(
                          width: 500,
                          padding: EdgeInsets.only(left: 20),
                          child: TextField(
                            controller: _beginTimeController,
                            style: TextStyle(fontSize: 22),
                            onTap: () async {
                              DateTime? tmpDate = await showDatePicker(
                                context: context,
                                initialDate: DateTime.now(),
                                firstDate: DateTime(2021),
                                lastDate: DateTime.now(),
                                locale: Locale.fromSubtags(languageCode: 'zh'),
                              );
                              if (tmpDate != null) {
                                _beginTimeController.text =
                                    Convert.format(tmpDate);
                              }
                            },
                            readOnly: true,
                            decoration: InputDecoration(
                                hintText: "请选择开始日期",
                                border: OutlineInputBorder()),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    child: Row(
                      children: [
                        Text(
                          "结束日期:",
                          style: TextStyle(
                              fontSize: 25,
                              fontWeight: FontWeight.bold,
                              color: Colors.blue),
                        ),
                        Container(
                          width: 500,
                          padding: EdgeInsets.only(left: 20),
                          child: TextField(
                            controller: _endTimeController,
                            style: TextStyle(fontSize: 22),
                            onTap: () async {
                              DateTime? tmpDate = await showDatePicker(
                                context: context,
                                initialDate: DateTime.now(),
                                firstDate: DateTime(2021),
                                lastDate: DateTime.now(),
                                locale: Locale.fromSubtags(languageCode: 'zh'),
                              );
                              if (tmpDate != null) {
                                _endTimeController.text =
                                    Convert.format(tmpDate);
                              }
                            },
                            readOnly: true,
                            decoration: InputDecoration(
                                hintText: "请选择结束日期",
                                border: OutlineInputBorder()),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              // width: 600,
              // color: Colors.blue,
              height: 50,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Container(
                    child: Row(
                      children: [
                        Text(
                          "操作人员:",
                          style: TextStyle(
                              color: Colors.blue,
                              fontSize: 25,
                              fontWeight: FontWeight.bold),
                        ),
                        Container(
                          width: 500,
                          padding: EdgeInsets.only(left: 20),
                          child: TextField(
                            controller: _operatorController,
                            style: TextStyle(fontSize: 22),
                            decoration: InputDecoration(
                              border: OutlineInputBorder(),
                              hintText: "请输入操作人员",
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    child: Row(
                      children: [
                        Text(
                          "通行人员:",
                          style: TextStyle(
                            color: Colors.blue,
                            fontSize: 25,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        Container(
                          width: 500,
                          padding: EdgeInsets.only(left: 20),
                          child: TextField(
                            controller: _passerbyController,
                            style: TextStyle(fontSize: 22),
                            decoration: InputDecoration(
                              border: OutlineInputBorder(),
                              hintText: "请输入通行人员",
                            ),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 60,
              width: 60,
              child: Visibility(
                visible: indicatorIsVisible,
                child: CircularProgressIndicator(),
              ),
            ),
          ],
        ),
      );

  void search() async {
    String beginTime = _beginTimeController.text;
    String endTime = _endTimeController.text;
    String operator = _operatorController.text;
    String passerby = _passerbyController.text;
    setState(() {
      indicatorIsVisible = true;
    });
    List<GateRecordModel>? lists =
        await GateRecordProvider.instance.queryByCondition(
      beginTime: beginTime,
      endTime: endTime,
      operator: operator,
      passerby: passerby,
    );
    setState(() {
      indicatorIsVisible = false;
    });

    // Navigator.of(context).pushNamed('/OperationRecord', arguments: lists);
    Navigator.push(context, SlideRoute(OpenGateRecord(recordLists: lists)));
  }
}
